package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;

import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder BCryptPasswordEncoder;
    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //sc完成验证后会将验证成功的用户信息保留
        org.springframework.security.core.userdetails.User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user!=null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //获取管理用户列表
    @GetMapping("/{page}/{limit}")
    public Result  userlist(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit ){
        PageResult result= userService.findUserList(page,limit);

        return new Result(true,"查询后台管理用户成功",result);
    }

    @PostMapping("/save")
    public Result save(@RequestBody com.itheima.pojo.User user,@RequestParam("roleIds") Integer[] roleIds ) {
        user.setPassword(BCryptPasswordEncoder.encode(user.getPassword()));
        userService.save(user,roleIds);
        return new Result(true,"新增成功");
    }

    @PostMapping("/update")
    public Result updateById(@RequestBody com.itheima.pojo.User user,@RequestParam Integer[] roleIds){
        user.setPassword(BCryptPasswordEncoder.encode(user.getPassword()));
        userService.updateById(user,roleIds);
        return new Result(true,"更新成功");
    }
    @RequestMapping("/remove/{id}")
    public Result removeById(@PathVariable("id") int id){
        userService.removeById(id);
        return new Result(true,"删除成功");
    }
    //根据用户id获取对应的角色
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable("userId")  int userId){
        Map<String, Set<Role>> map= userService.findRolesByUserId(userId);
        return new Result(true,"获取成功",map);
    }
   @GetMapping("/findUserById/{userId}")
   public Result findUserById(@PathVariable("userId") Integer userId){
       com.itheima.pojo.User user= userService.findUserById(userId);
        return new Result(true,"查询用户信息成功",user);
   }
   //根据用户查询对应的页面(特殊封装--由子菜单封装父菜单)
    @GetMapping("/getMenuByUserAndRole/{username}")
    public Result getMenuByUserAndRole(@PathVariable("username") String username){
        List<Menu> menuList= userService.getMenuByUserAndRole(username);
        return new Result(true,"查询用户对应的菜单成功",menuList);
    }

}
